package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.UsageStatusEnum;
import qc.module.ehs.dto.facility.FacilityDto;
import qc.module.ehs.entity.EhsFacilityBasic;

import java.util.List;

/**
 * EhsFacilityBasicMapper
 *
 * @author QuCheng Tech
 * @create 2024/9/23
 */
@Mapper
public interface EhsFacilityBasicMapper {
    
    EhsFacilityBasicMapper MAPPER = Mappers.getMapper(EhsFacilityBasicMapper.class);

    /**
     * entity to Dto
     * @param en
     * @return
     */
    @Mapping(target = "begintm", source = "begintm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "endtm", source = "endtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "status", source = "status.index")
    FacilityDto toDto(EhsFacilityBasic en);

    /**
     * List<entity> to List<Dto>
     * @param ens
     * @return
     */
    List<FacilityDto> toDtoList(List<EhsFacilityBasic> ens);

    /**
     * dto to entity
     * @param dto
     * @return
     */
    @Mapping(target = "begintm", source = "begintm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "endtm", source = "endtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "status", ignore = true)
    EhsFacilityBasic toEntity(FacilityDto dto);

    default EhsFacilityBasic dtoToEntity(FacilityDto dto) {
        EhsFacilityBasic en = EhsFacilityBasicMapper.MAPPER.toEntity(dto);

        en.setStatus(getUsageStatusEnum(dto.getStatus()));

        return en;
    }

    static UsageStatusEnum getUsageStatusEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (UsageStatusEnum item : UsageStatusEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认状态为0--未定义
        return UsageStatusEnum.UN_DEFINE;
    }
} 
